#ifndef NVERIFYCODE_H
#define NVERIFYCODE_H
#include <QtWidgets>
#include "tools/Gadget.h"


class NVerificationCode : public QMainWindow{

    Q_OBJECT

public:
    NVerificationCode(int startx = 5,
                                  int digit = 5,
                                  int polygon = 5,
                                  int pinum = 400,
                                  int BestowLine = 150,
                                  Qt::BrushStyle style = Qt::NoBrush,
                                  QBrush bkbrush = Qt::NoBrush,
                                  QWidget *parent = nullptr){

        backbrush = bkbrush;
        setContentsMargins(0, 0, 0, 0);
        setParent(parent);
        installEventFilter(this);
        RdoMap_Integet["startx"] = startx;
        RdoMap_Integet["digit"] = digit;
        RdoMap_Integet["polygon"] = polygon;
        RdoMap_Integet["pinum"] = pinum;
        RdoMap_Integet["BestowLine"] = BestowLine;
        RdoMap_Style["style"] = style;
        setAttribute(Qt::WA_OpaquePaintEvent);

    }

    bool eventFilter(QObject *watched, QEvent *event)
    {
        if ((event->type() == QEvent::MouseButtonPress &&
             static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton) ||
             event->type() == QEvent::Resize)
        {
            maskPixmap = QPixmap(size());
            maskPixmap.fill(Qt::transparent);
            repainter = true;
            update();
            return true;
        }
        else if (event->type() == QEvent::Paint)
        {
            return QMainWindow::eventFilter(watched, event);
        }
        else return true;
    }

    void paintEvent(QPaintEvent*){

        if (repainter)
        {
            QPainter pixPainter;
            pixPainter.begin(&maskPixmap);
            QList<QPoint> rect_point;
            QString getchar;
            QStringList collects;
            int rgb_end = 255;
            int rgb_start = 120;
            int randomx = 0;
            int randomy = 0;
            int reserved = 2;
            int random_fsize;
            int startx = RdoMap_Integet["startx"];
            int digit = RdoMap_Integet["digit"];
            int pinum = RdoMap_Integet["pinum"];
            int secwidth = width() / digit;
            pixPainter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
            if (!backbrush.texture().isNull() || backbrush.color().alpha())
            {
                pixPainter.setBrush(backbrush);
                pixPainter.drawRect(pixPainter.viewport());
            } else setStyleSheet("QMainWindow{background:transparent;}");

            if (RdoMap_Style["style"] != Qt::NoBrush)
            {
                QBrush BackgroundBrush(RdoMap_Style["style"]);
                pixPainter.setBrush(BackgroundBrush);
                pixPainter.drawRect(0, 0, width(), height());
            }

            for(int quanlity = 0; quanlity < RdoMap_Integet["polygon"]; quanlity++)
            {
                pixPainter.setPen(QPen(QColor(definfc.Getrandom(rgb_start, rgb_end),
                                              definfc.Getrandom(rgb_start, rgb_end),
                                              definfc.Getrandom(rgb_start, rgb_end))));

                if (width() <= 10)
                {
                    rect_point.append(QPoint(definfc.Getrandom(0, width()),
                                             definfc.Getrandom(0, height())));
                } else {
                    rect_point.append(QPoint(definfc.Getrandom(10, width()),
                                             definfc.Getrandom(10, height())));
                }
                pixPainter.drawPolygon(rect_point.toVector());
            }

            for(int n = 0; n < digit; n++)
            {
                getchar = definfc.AlphaArray()[definfc.Getrandom(0, definfc.AlphaArray().size() - 1)];
                random_fsize = definfc.Getrandom(height() / 3, height() - reserved - height() / 3);
                randomy = definfc.Getrandom(0, height() - random_fsize - reserved);
                qDebug() << randomy;
                pixPainter.setFont(QFont("KaiTi_GB2312", random_fsize));
                if (n == 0)
                {
                    randomx = definfc.Getrandom(reserved, secwidth);
                }
                else
                {
                    randomx = definfc.Getrandom(random_fsize / 2, secwidth);
                }

                QPen ramdompen = QPen(QColor(qRgb(definfc.Getrandom(rgb_start, rgb_end),
                                                  definfc.Getrandom(rgb_start, rgb_end),
                                                  definfc.Getrandom(rgb_start, rgb_end))));
                ramdompen.setWidth(3);
                pixPainter.setPen(ramdompen);
                startx += randomx;
                QRect txrect(startx, randomy, width(), height());
                pixPainter.setBrush(QColor(200,200,200));
                pixPainter.drawRect(txrect);
                pixPainter.drawText(txrect,
                                            Qt::AlignLeft | Qt::AlignTop,
                                            getchar);
                collects.append(getchar);

            }
            for(int n = 0; n < pinum / 2; n++)
            {
                QPen PointPen = QPen(QColor(qRgb(definfc.Getrandom(rgb_start, rgb_end),
                                                 definfc.Getrandom(rgb_start, rgb_end),
                                                 definfc.Getrandom(rgb_start, rgb_end))));
                PointPen.setWidthF(1);
                int point_x1 = definfc.Getrandom(0, width());
                int point_y2 = definfc.Getrandom(0, height());
                int point_x3 = definfc.Getrandom(0, width());
                int point_y4 = definfc.Getrandom(0, height());
                pixPainter.setPen(PointPen);
                pixPainter.drawPoint(point_x1, point_y2);
                pixPainter.drawPoint(point_x3, point_y4);
                if (n % RdoMap_Integet["BestowLine"] == 0)
                {
                    pixPainter.drawLine(point_x1, point_y2, point_x3, point_y4);
                }
            }
            emit qrcode_send(collects.join(""));
            repainter = false;
            pixPainter.end();
        }
        QPainter mainPainter(this);
        mainPainter.drawPixmap(mainPainter.viewport(), maskPixmap);
    }

private:
    Extend definfc;
    bool repainter = true;
    QPixmap maskPixmap;
    QBrush backbrush;
    QMap<QString, int> RdoMap_Integet;
    QMap<QString, Qt::BrushStyle> RdoMap_Style;

signals:
    void qrcode_send(QString);

};

#endif
